<?php 
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use cmf\controller\BaseController;
use think\Db;

	class WxsiteController extends BaseController
	{

		public function index(){
			// $weekarray=array("日","一","二","三","四","五","六");
			// // echo date("Y-m-d",strtotime('+1 day'));
			// // exit;
			// strtotime(date('Y-m-d'));
			// echo $weekarray[date("w",time())];

			$where['s.status']=1;
			$incubator_id = $this->request->param('incubator_id',1);
			if($incubator_id){
				$where['s.incubator_id']=$incubator_id;	
			}	
			$where['incubator_id']=1;	
			$site_types = $this->request->param('site_type_id','');

			if($site_types){
				$where['site_type_id'] = $site_types;	
			}
			/**搜索条件**/
			$site_name = $this->request->param('site_name');
			$address = $this->request->param('address');

			if ($site_name) {
				$where['site_name'] = ['like', "%$site_name%"];
			}

			if ($address) {
				$where['s.address'] = ['like', "%$address%"];
			}
			$equipments=$this->equipments();
			 // get_cate_name(5,$vo['site_type_id']);
			// $list=Db::name('site')->alias('s')->where($where)->join('cx_site_apply_info p','p.site_id=s.id')->join('incubator i','i.id=s.incubator_id')->field('s.id,s.incubator_id,s.site_name,s.site_type_id,s.price,s.address,s.people,s.mobile,s.site_sketch,s.pic,s.is_rent,s.start_time,s.end_time,s.status,i.pic_url,i.incubator_name,p.start_time,p.end_time')->order('id','desc')->select();
			$list=Db::name('site')->alias('s')->where($where)->join('incubator i','i.id=s.incubator_id')->field('s.id,s.site_name,s.site_type_id,s.start_time as stime,s.equipments,s.end_time as etime,is_audit,s.pic')->order('id','desc')->select();
				$list=json_decode(json_encode($list),true);
				// var_dump($list);
				// exit;
			$i=8;
			$j=0;
			$s=0;
			for($i;$i<=20;$i++){
				for($s=0;$s<2;$s++){
					$f=$s*30;
					if(!$f){
						$f='00';
						// echo $f;
					}
					// $j=$s*30;
					$times[] = $i.':'.$f;
					
				}
			}
		
			//var_dump($list);
			//exit;
			foreach ($list as $key => $value) {
				//var_dump($time);
				$ss=explode(':',$value['stime']);
				$ee=explode(':',$value['etime']);

				// $value['stime']
				// echo $ee[0];
				// echo $ss[0];
				// exit;
				$arr[]=$value['stime'];
				$arr[]=$value['etime'];
				if($ss[1] !=30 ){
					$arr[]=$ss[0].':30';
				}
				if($ee[1] !=0 ){
					$arr[]=$ee[0].':00';
				}
				// var_dump($arr);
				// $arr[]=$value['etime'];
				$count=$ee[0]-$ss[0];
				for($g=1;$g<$count;$g++){
					$arr[]=$ss[0]+$g.':00';
					$arr[]=$ss[0]+$g.':30';
					
				}
				//var_dump($arr);
				foreach ($times as $keys => $values) {
					if(in_array($values, $arr)){
						$list[$key]['apply'][]=array('selected'=>3,'time'=>$values);
					}else{
						$list[$key]['apply'][]=array('selected'=>1,'time'=>$values);
					}
				}
				unset($arr);
				 // var_dump($show);
				 //var_dump($time);
				//exit;
			//	echo $ee[0];
				// foreach ($time as $k => $v) {

				// 	if($value['stime']==$v){
				// 		echo $v;

				// 		exit;
				// 	}
				// }
			}
			//var_dump($list[0]['apply']);
			// exit;
			// p.start_time,p.end_time
			// $list=json_decode(json_encode($list),true);
			$day=$this->request->param('num');
			for($i=0;$i<count($list);$i++){
				$arr=explode(',', $list[$i]['equipments']);
				/*当天*/
				$time[1]['s']=strtotime(date("Y-m-d"));
				$time[1]['e']=strtotime(date("Y-m-d",strtotime('+1 day')));
				$time[2]['s']=strtotime(date("Y-m-d",strtotime('+1 day')));
				$time[2]['e']=strtotime(date("Y-m-d",strtotime('+2 day')));
				$time[3]['s']=strtotime(date("Y-m-d",strtotime('+2 day')));
				$time[3]['e']=strtotime(date("Y-m-d",strtotime('+3 day')));
				$time[4]['s']=strtotime(date("Y-m-d",strtotime('+3 day')));
				$time[4]['e']=strtotime(date("Y-m-d",strtotime('+4 day')));
				$time[5]['s']=strtotime(date("Y-m-d",strtotime('+4 day')));
				$time[5]['e']=strtotime(date("Y-m-d",strtotime('+5 day')));
				$time[6]['s']=strtotime(date("Y-m-d",strtotime('+5 day')));
				$time[6]['e']=strtotime(date("Y-m-d",strtotime('+6 day')));
				$time[7]['s']=strtotime(date("Y-m-d",strtotime('+6 day')));
				$time[7]['e']=strtotime(date("Y-m-d",strtotime('+7 day')));
				foreach ($time as $key => $value) {
					if($key==$day){
						// echo $day;
						// exit;
						// $where2=' staff_id = "'.$data['id'].'" ' ;
						// $condition1='(is_audit = 1 ) or (is_audit =2 and audit_status =2)';
						$condition='audit_status = 2';
						 $where_apply['site_id']=$list[$i]['id'];
						//$where_apply['site_id']=1;
						$where_apply['start_time']=array('egt',$value['s']);
						$where_apply['end_time']=array('elt',$value['e']);
						$apply_info=db('site_apply_info')->where($where_apply)->where($condition)->field('id,start_time,end_time,staff_id')->select();
					//	var_dump($apply_info);
						//var_dump($list[$i]);
						// echo db('site_apply_info')->getLastSql();
						// exit;
						$apply_info=json_decode(json_encode($apply_info),true);
						// var_dump($apply_info);
						// exit;
						//echo 222222222;
						foreach ($apply_info as $ks => $vs) {
							$stime= date("H:i",$vs['start_time']);
							$etime= date("H:i",$vs['end_time']);

							$ss=explode(':',$stime);
							$ee=explode(':',$etime);

				// $value['stime']
				// echo $ee[0];
				// echo $ss[0];
				// exit;
							$arrs[]=$stime;
							$arrs[]=$etime;
							if($ss[1] !=30 ){
								$arrs[]=$ss[0].':30';
							}
							if($ee[1] !=0 ){
								$arrs[]=$ee[0].':00';
							}
				// var_dump($arr);
				// $arr[]=$value['etime'];
							$count=$ee[0]-$ss[0];
							for($g=1;$g<$count;$g++){
								$arrs[]=$ss[0]+$g.':00';
								$arrs[]=$ss[0]+$g.':30';

							}
							$count=count($arrs);	
							// var_dump($arrs);
							// exit;
							//foreach ($list as $k1 => $v1) {
								// if($v1['apply'])$list[$i]['apply']
							//var_dump($list[$i]['apply']);
							//var_dump($arrs);
							//exit;
							// var_dump($list[$i]);
							// exit;
							    $last_num=1;
								foreach ($list[$i]['apply'] as $k2 => $v2) {
									 //echo $v2['time'].'@@@';
									// exit;
									// var_dump($arrs);
									// exit;
									//var_dump($arrs);
									// var_dump($v2).'*****';
									
									if(in_array($v2['time'], $arrs) || in_array('0'.$v2['time'], $arrs)){
										//echo $last_num.'###';
										if($last_num==$count){
											$list[$i]['apply'][$k2]['last']=1;
										}	
									//	echo $v2['time'].'@@@';
										//var_dump($arrs);
										//echo '<br/>';
										$list[$i]['apply'][$k2]['staff_id']=$vs['staff_id'];
										$list[$i]['apply'][$k2]['staff_name']=db('staff_member')->where('id',$vs['staff_id'])->value('staff_name');
										$list[$i]['apply'][$k2]['selected']=2;
										$last_num++;
										//echo 333333333333;
									}
									
								}
								 unset($arrs);
								 //var_dump($list[$i]['apply']);
								// var_dump($vs);
								// exit;
							//}
							// var_dump($list[0]['apply']);
							// exit;









						}
					//	var_dump($list[2]);
						//exit;
						// $list[$i]['apply_time_info'][]=$apply_info;
					}
				
					// if($list[$i]['start_time'] >= $value['s'] && $list[$i]['end_time']<=$value['e'] ){

					// }
					# code...
				}

				for($s=0;$s<count($arr);$s++){
					
					$list[$i]['equipment'][]=get_cate_name(16,$arr[$s]);

				}
				$url=cmf_get_image_preview_url($list[$i]['pic']);
				$list[$i]['pic']=str_replace('http:','https:',$url);
				$list[$i]['site_type']=site_type(5,$list[$i]['site_type_id']);
			}
			// var_dump($list[2]['apply_time_info']);
//var_dump($list[1]);
			return json($list);
			// foreach ($list as $key => $value) {
			// 	# code...
			// 	$arr=explode(',', $value['equipments']);
			// 	foreach ($arr as $k => $v) {
			// 		// echo get_cate_name(16,$v);
			// 		// exit;
			// 		// echo $list[$key]['equipments'];exit;
			// 		echo $list[$key]['a'];exit;
			// 		$list[$key]['a']=get_cate_name(16,$v);
			// 	}
				
			// }
			// var_dump($list);
			// get_cate_name($type_id,$cate_id)
			// $site_type=$this->site_type();

		
		
		}
		/*添加会议室预约*/
		function add_apply_site(){
			$data['site_id'] = $this->request->param('site_id');
		//	$data['site_id'] = $this->request->param('choose_num');
			$data['staff_id'] = $this->request->param('staff_id');
			$data['start_time'] = $this->request->param('start_time');
			$data['end_time'] = $this->request->param('end_time');
			$add=db('site_apply_info')->insert($data);
			if($add){
				return json(array('status'=>1,'mess'=>'预约审核中'));
			}else{
				return json(array('status'=>0,'mess'=>'预约失败'));
			}


		}
		/*会议室取消预约*/
		function cancel_apply_site(){
		// 	$data['site_id'] = $this->request->param('site_id');
		// //	$data['site_id'] = $this->request->param('choose_num');
		// 	$data['staff_id'] = $this->request->param('staff_id');
		// 	$data['start_time'] = $this->request->param('start_time');
		// 	$data['end_time'] = $this->request->param('end_time');
			$data['id'] = $this->request->param('apply_id');
			$add=db('site_apply_info')->where($data)->update(array('is_using'=>6,'audit_status'=>3));
			if($add){
				return json(array('status'=>1,'mess'=>'取消成功'));
			}else{
				return json(array('status'=>0,'mess'=>'取消失败'));
			}


		}
		/*员工会议室预约列表 type ==1 预约成功 2进行中3取消*/
		function get_apply_list(){
			$where=array();
			$condition='';
			$data['staff_id'] = $this->request->param('staff_id');
			$data['type'] = $this->request->param('type');
			if($data['type']==1){
				$where=' staff_id = "'.$data['staff_id'].'" and start_time > "'.time().'" ' ;
				$condition='audit_status =2';
			}
			if($data['type']==2){
				$where=' staff_id = "'.$data['staff_id'].'" and start_time < "'.time().'"  and end_time > "'.time().'" ' ;
				$condition='(is_audit = 1 ) or (is_audit =2 and audit_status =2)';
			}
			if($data['type']==3){
				$where=' staff_id = "'.$data['staff_id'].'" ' ;
				$condition='is_using = 6';
			}
			$apply_list=db('site_apply_info')->where($where)->where($condition)->field('id as apply_id,site_id,start_time,end_time')->select();
			$apply_list = json_decode(json_encode($apply_list),true);
			foreach ($apply_list as $key => $value) {
				$site_info=db('site')->where('id',$value['site_id'])->field('site_name,pic')->find();
				$apply_list[$key]['site_name']=$site_info['site_name'];
				$url=cmf_get_image_preview_url($site_info['pic']);
				$apply_list[$key]['site_img']=str_replace('http:','https:',$url);

				

				
			}
			// echo db('site_apply_info')->getLastSql();
			// var_dump($apply_list);
			
			return json($apply_list);
		}
		/*取消会议室*/

		/*设备*/
		function equipments(){
			$where=array('t.name'=>'设备');
			$list=db("category_type")->alias('t')->join('category c','t.id=c.type_id')->field('c.id,c.name')->where($where)->select();
			return $list;
		}

		/*园区添加*/		
		public function addSite(){
			//$_POST['start_time']='0000-00-00 00:00:00';
			if($_POST){
			
				$data['site_name'] = $this->request->param('site_name','');
				$data['incubator_id'] = $this->request->param('incubator_id',0,'intval');
				$data['site_type_id'] = $this->request->param('site_type_id',1);
				$data['price'] = $this->request->param('price',0);
				$data['mobile'] = $this->request->param('mobile','');
				$data['people'] = $this->request->param('people',0,'intval');	
			//场地正常一天开始到结束的时间
				
				// echo $time;
				// exit;
				$data['start_time'] = $this->request->param('start_time','');
				$data['end_time'] = $this->request->param('end_time','');
				$data['pic'] = $this->request->param('pic','');
				$data['pic'] = $this->request->param('pic','');
				$data['site_sketch'] = $this->request->param('site_sketch','');
				$data['address'] = $this->request->param('address');
				$data['status'] = $this->request->param('status',1);
				$data['sort'] = $this->request->param('sort',0);
				if(!$data['start_time']){
					unset($data['start_time']);
				}
				if(!$data['end_time']){
					unset($data['end_time']);
				}
			// echo $incubator_name ;
			// if($type_id==1){
			// 	$station_	
			// }					

			// echo $type_id;
	// var_dump($data);
	// 			exit;
				$add=Db::name('site')->insert($data);

				if($add){
					$this->success('添加成功','sit/index');
				}else{
					$this->error('添加失败');
				}	
			}else{
				$where=array();	
				if(session('incubator_id')){
					$where['id']=session('incubator_id');
				}
				$incubator_list=db('incubator')->where(array('status'=>1))->where($where)->field('id,incubator_name')->select();
				$site_type=$this->site_type();
				$this->assign('site_type',$site_type);
				$this->assign('incubator_list',$incubator_list);
				return $this->fetch();
			}
			





		}
		/*场地类型*/
		function site_type(){
			// echo config('img_host');
			
			// exit;

			$where=array('t.name'=>'场地类型');
			$list=db("category_type")->alias('t')->join('category c','t.id=c.type_id')->field('c.id,c.name,c.img')->where($where)->select();
			$list=json_decode(json_encode($list),true);
		
			foreach ($list as $key => $value) {
				if($value['img']){
					$list[$key]['img']=config('img_host').$value['img'];
				}
				
			}
		
			return json($list);
		}
		/*详情*/
		public function detailSite(){
			$where=array();
			$id= $this->request->param('id',0,'intval');
			if($id){
				$where['cx_site.id']= $id;
			}else{
				// dump(info_back('','id为空',false));
				// exit;
				return info_back('','id为空',false);
			}
		//	$detail= Db::name('site')->where($where)->join('cx_incubator','cx_site.incubator_id=cx_incubator.id')->find();
			$detail= Db::name('site')->where($where)->find();
			//var_dump($detail);
			$where=array();	
			if(session('incubator_id')){
				$where['id']=session('incubator_id');
			}
			$incubator_list=db('incubator')->where(array('status'=>1))->where($where)->field('id,incubator_name')->select();
			
			$site_type=$this->site_type();
			$this->assign('site_type',$site_type);
			$this->assign('incubator_list',$incubator_list);
			$this->assign('detail',$detail);
			return $this->fetch();
		}

		/*编辑园区*/	
		public function editSite(){
			if($_POST){
				// dump($_POST);
				// exit;
				$edit = Db::name('site')->update($_POST);
				if($edit){
					$this->success('编辑成功','Sit/index');
				}else{
					$this->success('编辑成功','Sit/index');
				}
			}
			$where=array();
			$id= $this->request->param('id',0,'intval');
			if($id){
				$where['id']= $id;
			}else{
				$this->error('该信息id不能为空');
			}
			// echo $id;
			// exit;
			//dump($where);
			$detail= Db::name('site')->where($where)->find();
			//dump($detail);
			//exit;
			$where=array();	
			if(session('incubator_id')){
				$where['id']=session('incubator_id');
			}

			$incubator_list=db('incubator')->where(array('status'=>1))->where($where)->field('id,incubator_name')->select();
			
			$site_type=$this->site_type();

			$this->assign('site_type',$site_type);
			$this->assign('incubator_list',$incubator_list);
			$this->assign('detail',$detail);
			return $this->fetch();

		}


		/*删除园区*/
		public function delSite(){
			$data['id']= $this->request->param('id',0,'intval');

			$data['status']=0;

			$del=Db::name('site')->update($data);
			if($del){
				return 1;
			}else{
				return 0;
			}

		}	








































}